﻿/*************************   Auto Generated Repository Code For Vital table     Generated @ 5/7/2013 12:23:03 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class VitalRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an Vital by its VitalID (Primary Key)
        /// </summary>
        /// <param name="VitalID">The ID of the <see cref="Vital"/> to retrieve</param>
        /// <returns>A <see cref="Vital"/> object</returns>
        public BusinessObjects.Vital Select(int VitalID)
        {
            Vital retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Vitals where x.VitalID == VitalID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of Vital objects
        /// </summary>
        /// <returns>A List of type <see cref="Vital"/></returns>
        public List<BusinessObjects.Vital> SelectAll()
        {
            List<BusinessObjects.Vital> retObject = new List<BusinessObjects.Vital>();

            using (dB = new PracticeSimpleDataContext())
            {
                var VitalList = (from x in dB.Vitals select x).ToList();

                foreach (Vital obj in VitalList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a Vital Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public void Save(BusinessObjects.Vital businessObject)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                dB.Vitals.InsertOnSubmit(ConvertToDataObject(businessObject));
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Accepts the Primary Key of a Vital, finds it in the database and deletes it
        /// </summary>
        /// <param name="VitalID">The ID of the <see cref="Vital"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int VitalID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.Vital obj = (from x in dB.Vitals where x.VitalID == VitalID select x).SingleOrDefault();
                dB.Vitals.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a Vital data object to a Vital business object
        /// </summary>
        /// <returns>A business object of type <see cref="Vital"/></returns>
        public BusinessObjects.Vital ConvertToBusinessObject(DataLayer.Vital dataObject)
        {
            BusinessObjects.Vital businessObject = new BusinessObjects.Vital();

            businessObject.VitalID = dataObject.VitalID;
            businessObject.VitalName = dataObject.VitalName;
            businessObject.InputTypeID = dataObject.InputTypeID;
            businessObject.VariableTypeID = dataObject.VariableTypeID;
            businessObject.ParentVitalID = dataObject.ParentVitalID;


            return businessObject;

        }

        /// <summary>
        /// Converts a Vital business object to a Vital data object
        /// </summary>
        /// <returns>A data object of type <see cref="Vital"/></returns>
        public DataLayer.Vital ConvertToDataObject(BusinessObjects.Vital businessObject)
        {
            DataLayer.Vital dataObject = new DataLayer.Vital();

            dataObject.VitalID = businessObject.VitalID;
            dataObject.VitalName = businessObject.VitalName;
            dataObject.InputTypeID = businessObject.InputTypeID;
            dataObject.VariableTypeID = businessObject.VariableTypeID;
            dataObject.ParentVitalID = businessObject.ParentVitalID;


            return dataObject;
        }
    }
}


